package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.ApartmentInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.ApartmentInfo;
import com.atguigu.lease.model.vo.ApartmentDetailVo;
import com.atguigu.lease.model.vo.ApartmentItemVo;
import com.atguigu.lease.model.vo.ApartmentQueryVo;
import com.atguigu.lease.model.vo.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "公寓管理模块")
@RestController
@RequestMapping("admin/apartment")
public class ApartmentController {

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Operation(summary = "5.更新公寓的发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id,
                                          @RequestParam String status) {
        apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId, id)
                .eq(ApartmentInfo::getIsRelease,status));
        return Result.ok();

    }

    @Operation(summary = "4.删除公寓")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam("id") Long id) {
        apartmentInfoService.deleteApartmentById(id);
        return Result.ok();
    }

    @Operation(summary = "3.查询公寓详情")
    @GetMapping("getDetailById")
    public Result getDetailById(@RequestParam("id") Long id) {
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "2.查询公寓分页列表")
    @GetMapping("pageItem")
    public Result<Page<ApartmentItemVo>> pageItem(@RequestParam Long current,
                                                  @RequestParam Long size,
                                                  ApartmentQueryVo queryVo){
        Page<ApartmentItemVo> page = apartmentInfoService.getPageItem(current,size,queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "1.新增或更新公寓")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo){
        apartmentInfoService.saveOrUpdateApartmentSubmitVo(apartmentSubmitVo);
        return Result.ok();
    }
}
